*****************************************************************************************************************************************************************
* STUDY 2
* Replication code for "It’s not over when it’s over" - Post-decision arrangements and empirical legitimacy, Journal of Public Administration Research and Theory 
* Authors: Jenny de Fine Licht, University of Gothenburg, jenny.definelicht@spa.gu.se - Peter Esaiasson, University of Gothenburg, peter.esaiasson@pol.gu.se - 
* Mattias Agerberg, University of Gothenburg, mattias.agerberg@gu.se
******************************************************************************************************************************************************************

use "... study 2 data.dta", clear 
cd "..."
local path "..."


************************************************************************************************************************************************
****** DATA MANAGEMENT ******
************************************************************************************************************************************************

// Clean up
drop t_q* do_* 

// Outcome variables
replace q27 = (q27-1)/4
replace q28 = (q28-1)/4
replace q29 = (q29-1)/4

alpha q27 q28 q29, item
gen outcome_combined = q27+q28+q29
replace outcome_combined = outcome_combined/3
label var outcome_combined "Perceived legitimacy"

gen outcome_binary = .
replace outcome_binary = 1 if outcome_combined >= 0.5
replace outcome_binary = 0 if outcome_combined < 0.5
label var outcome_binary "Overall legitimate decision"

// Treatments
* Pre-decision voice
recode s1gr1 (1=0) (2=1) 
label define s1gr1_lab 0 "Invitation to dialogue" 1 "No invitation", add 
label values s1gr1 s1gr1_lab
label var s1gr1 "Pre-decision dialogue"

* Announcement
recode s1gr2 (2=1) (1=0)
label define s1gr2_lab 1 "Direct" 0 "Indirect", add 
label values s1gr2 s1gr2_lab
label var s1gr2 "Announcement"

* Explanation
recode s1gr3 (3=0) (2=4) (1=2) 
recode s1gr3 (4=1) 
label define s1gr3_lab 0 "No explanation" 1 "Premises, equipment, and staff" 2 "Budgetary constraints", add
label values s1gr3 s1gr3_lab
label var s1gr3 "Principled explanation"

* Post-decision dialogue
recode s1gr4 (3=0)
label define s1gr4_lab 1 "General" 2 "Individual meetings" 0 "No invitation", add 
label values s1gr4 s1gr4_lab
label var s1gr4  "Post-decision dialogue"

* Special funds
recode s1gr5 (2=0)
label define s1gr5_lab 1 "Special funds" 0 "No special funds", add 
label values s1gr5 s1gr5_lab
label var s1gr5 "Special funds"

* Democratic process
recode s1gr6 (2=0) 
label define s1gr6_lab 1 "Ref. democratic process" 0 "No reference", add 
label values s1gr6 s1gr6_lab
label var s1gr6 "Democratic process"

// Covariates
gen high_pol_interest = .
replace high_pol_interest = 0 if polint >= 2
replace high_pol_interest = 1 if polint == 1
label var high_pol_interest "High political interest"

label define sex_lab 1 "Female" 2 "Male", add 
label values sex sex_lab
label var sex "Gender"
gen female = sex
recode female (2=0)

label var lrpown "Left-right" 
replace lrpown = lrpown/10

gen university_ed = 0
replace university_ed = 1 if edu >= 7
replace university_ed = . if edu == .
label var university_ed "University education"

gen high_pol_trust = .
replace high_pol_trust = 0 if inrange(poltrust,2,4)
replace high_pol_trust = 1 if poltrust == 1
label var high_pol_trust "High political trust"

gen under_40 = .
replace under_40 = 0 if inrange(age6,3,6)
replace under_40 = 1 if inrange(age6,1,2)
label var under_40 "Under 40"

// Descriptives
tabstat under_40 university_ed female high_pol_interest high_pol_trust lrpown, s(mean sd min max n) format(%10.3fc) c(statistics)


************************************************************************************************************************************************
****** MAIN MANUSCRIPT ******
************************************************************************************************************************************************

// Graphs
***(Figure 1)*** 
* Histogram: Initial reaction
hist q14, scheme(s2mono) xlabel(1 `" "Completely" "unreasonable" "' 2 `" "Quite" "unreasonable" "' 3 `" "Neither reasonable" "nor unreasonable" "' 4 `" "Quite" "reasonable" "' 5 `" "Completely" "reasonable" "') /// 
frac ytitle("Density", margin(medsmall)) title("Spontaneous reactions to decision", margin(medsmall)) xtitle(" ") graphregion(margin(l+0 r+4 t+0 b+0)) 
graph export  "`path'initial_reaction.pdf", replace

* Histogram: Initial reaction, study 1 + 2
* Study 2
hist q14, scheme(s2mono) xlabel(1 `" "Completely" "unreasonable" "' 2 `" "Quite" "unreasonable" "' 3 `" "Neither reasonable" "nor unreasonable" "' 4 `" "Quite" "reasonable" "' 5 `" "Completely" "reasonable" "') /// 
frac ytitle("Density", margin(medsmall)) title("Study 2: The directly affected public", margin(medsmall)) xtitle(" ") graphregion(margin(l+0 r+4 t+0 b+0)) name(hist2, replace)

* Study 1
use "... study 1 data.dta", clear

hist q21, scheme(s2mono) xlabel(1 `" "Completely" "unreasonable" "' 2 `" "Quite" "unreasonable" "' 3 `" "Neither reasonable" "nor unreasonable" "' 4 `" "Quite" "reasonable" "' 5 `" "Completely" "reasonable" "') /// 
frac ytitle("Density", margin(medsmall)) title("Study 1: The general public", margin(medsmall)) xtitle(" ") graphregion(margin(l+0 r+4 t+0 b+0)) name(hist1, replace)

gr combine hist1 hist2, scheme(s2mono) xsize(8.8) ysize(4) scale(1.03)
* graph export  "`path'hist_combined.pdf", replace

***(Figure 4)*** 
* Pre-process: Spontaneous reaction
reg q14 i.s1gr1 i.s1gr2, robust

coefplot, base drop(_cons) xline(0, lp(dash) lc(gs8)) scheme(s2mono) mc(black) /// 
headings(0.s1gr1 = "{bf:Pre-decision voice}" 0.s1gr2 = "{bf:Announcement}") ///
xtitle(Coefficient, margin(medsmall)) title("Spontaneous reactions to decision", margin(medsmall)) 
* graph export  "`path'preprocess_reaction.pdf", replace

***(Figure 5)*** 
* Main treatments graph, combined outcomes
reg outcome_combined i.s1gr2 i.s1gr3 i.s1gr6 i.s1gr4 i.s1gr5 i.s1gr1, robust

coefplot, base drop(_cons) xline(0, lp(dash) lc(gs8)) scheme(s2mono) mc(black) /// 
headings(0.s1gr3 = "{bf:Substantial explanation}" 0.s1gr4 = "{bf:Post-decision voice}" 0.s1gr6 = "{bf:Principled explanation}"  /// 
0.s1gr5 = "{bf:Alleviate neg. consequences}" 0.s1gr2 = "{bf:Announcement}" 0.s1gr1 = "{bf:Pre-decision voice}") ///
groups(0.s1gr2 1.s1gr5 = "{bf: Post-decision}" 0.s1gr1 1.s1gr1 = "{bf: Pre-decision}", angle(rvertical)) yscale(alt axis(2)) ///
ysize(6.2) graphregion(margin(l+6 r+2 t+0 b+0)) scale(0.88) ///
xtitle(Coefficient, margin(medsmall)) title(Empirical legitimacy, margin(medsmall)) 
* graph export  "`path'main_treatments.pdf", replace

***(Figure 6)*** 
* Main treatments graph, best post process (binary)
reg outcome_binary i.s1gr1 i.s1gr2 i.s1gr3 i.s1gr4 i.s1gr5 i.s1gr6, robust

margins, at(s1gr3==(2) s1gr4==(2) s1gr5==(1) s1gr6==(1)) at(s1gr3==(1) s1gr4==(0) s1gr5==(0) s1gr6==(0))  
marginsplot, recast(scatter) scheme(s2mono) plotregion(margin(l+15 r+15)) plot1opts(mcolor(black) ms(circle)) graphregion(margin(l+5 r+5)) ci1op(lc(black)) /// 
xlabel(1 `" "Worst" "procedure" "' 2 `" "Best" "procedure" "') xtitle(" ") ytitle("Expected share", margin(medsmall)) title("Empirical legitimacy: midpoint or above", margin(medsmall)) xsize(4) ysize(4) ///
yscale(r(0 1)) ylabel(0 "0" 0.1 " " 0.2 ".2" 0.3 " " 0.4 ".4" 0.5 " " 0.6 ".6" 0.7 " " 0.8 ".8" 0.9 " " 1 "1") yline(0.5, lcolor(gs12))
* graph export  "`path'best_postprocess_legitimate.pdf", replace


************************************************************************************************************************************************
****** APPENDIX ******
************************************************************************************************************************************************

* Appendix B: Moderating effect of pre-decision arrangements 
*  Dialogue
reg outcome_combined i.s1gr1 i.s1gr2 i.s1gr3 i.s1gr4 i.s1gr5 i.s1gr6
est store a

reg outcome_combined i.s1gr1 i.s1gr2 i.s1gr3##i.s1gr1 i.s1gr4##i.s1gr1 i.s1gr5##i.s1gr1 i.s1gr6##i.s1gr1
est store b

ftest a b

* Announcement
reg outcome_combined i.s1gr1 i.s1gr2 i.s1gr3 i.s1gr4 i.s1gr5 i.s1gr6
est store a

reg outcome_combined i.s1gr1 i.s1gr2 i.s1gr3##i.s1gr2 i.s1gr4##i.s1gr2 i.s1gr5##i.s1gr2 i.s1gr6##i.s1gr2
est store b

ftest a b

* Appendix B: Predicting empirical legitimacy with spontaneous reaction 
reg outcome_combined i.q14, robust

margins, at(q14==(1(1)5))
marginsplot, recast(scatter) scheme(s2mono) graphregion(margin(l+3 r+3)) plot1opts(mcolor(black) ms(circle))  ci1op(lc(black)) /// 
xlabel(1 `" "Completely" "unreasonable" "' 2 " " 3 "Neutral" 4 " " 5 `" "Completely" "reasonable" "') /// 
xtitle("Decision: spontaneous reaction", margin(medsmall)) ytitle("Predicted empirical legitimacy", margin(medsmall)) title("Study 2: The directly affected public", margin(medsmall)) ///
yscale(r(0 1)) ylabel(0 "0" 0.25 ".25" 0.5 ".5" 0.75 ".75" 1 "1") name(spontaneous2, replace)
graph export  "`path'predicted_legitimacy_initial.pdf", replace

* Combine w Study 1
use "... study 1 data.dta", clear

reg outcome_combined i.q21

margins, at(q21==(1(1)5))
marginsplot, recast(scatter) scheme(s2mono) graphregion(margin(l+3 r+3)) plot1opts(mcolor(black) ms(circle))  ci1op(lc(black)) /// 
xlabel(1 `" "Completely" "unreasonable" "' 2 " " 3 "Neutral" 4 " " 5 `" "Completely" "reasonable" "') /// 
xtitle("Decision: spontaneous reaction", margin(medsmall)) ytitle("Predicted empirical legitimacy", margin(medsmall)) title("Study 1: The general public", margin(medsmall)) ///
yscale(r(0 1)) ylabel(0 "0" 0.25 ".25" 0.5 ".5" 0.75 ".75" 1 "1") name(spontaneous1, replace)

gr combine spontaneous1 spontaneous2, scheme(s2mono) xsize(8.8) ysize(4) scale(1.03)
graph export  "`path'spontaneous_combined.pdf", replace

